{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b6be365-b8e3-4e39-a33a-90c5547b3f8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "DATA_PATH = r\"\\df_1.xlsx\"\n",
    "df = pd.read_excel(DATA_PATH)\n",
    "df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
    "df = df.sort_values(\"date\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a29ac28-42b4-4a34-8353-54ef5ff99c4c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9861bd8-0b1b-478f-a4a4-d133ef1f58a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Series (monthly grievance share: 0–1)\n",
    "y_nk = df[\"grievance_NorthKorea\"].astype(float)\n",
    "y_cn = df[\"grievance_China\"].astype(float)\n",
    "\n",
    "# -----------------------------\n",
    "# Trend (rolling mean)\n",
    "# -----------------------------\n",
    "WIN = 12  # 12-month rolling trend (adjust if desired)\n",
    "trend_nk = y_nk.rolling(window=WIN, min_periods=6, center=True).mean()\n",
    "trend_cn = y_cn.rolling(window=WIN, min_periods=6, center=True).mean()\n",
    "\n",
    "# -----------------------------\n",
    "# Plot settings\n",
    "# -----------------------------\n",
    "FIGSIZE = (14, 5)\n",
    "TICK_SIZE = 14\n",
    "LABEL_SIZE = 16\n",
    "LEGEND_SIZE = 14\n",
    "LINE_WIDTH = 2.0\n",
    "TREND_WIDTH = 2.6\n",
    "\n",
    "# Keep distinct dashed styles, now used for TRENDS (not raw series)\n",
    "STYLE_NK = (0, (3, 3))      # dashed\n",
    "STYLE_CN = (0, (1, 2))      # dotted-like\n",
    "COLOR_NK = \"blue\"\n",
    "COLOR_CN = \"red\"\n",
    "\n",
    "# -----------------------------\n",
    "# Create figure\n",
    "# -----------------------------\n",
    "fig, ax = plt.subplots(figsize=FIGSIZE)\n",
    "\n",
    "# Raw series (SOLID)\n",
    "ax.plot(df[\"date\"], y_nk, linestyle=\"-\", linewidth=LINE_WIDTH, color=COLOR_NK,\n",
    "        alpha=0.8, label=\"North Korea (monthly)\")\n",
    "ax.plot(df[\"date\"], y_cn, linestyle=\"-\", linewidth=LINE_WIDTH, color=COLOR_CN,\n",
    "        alpha=0.8, label=\"China (monthly)\")\n",
    "\n",
    "# Trend series (DASHED / DOTTED)\n",
    "ax.plot(df[\"date\"], trend_nk, linestyle=STYLE_NK, linewidth=TREND_WIDTH, color=COLOR_NK,\n",
    "        label=f\"North Korea (trend, {WIN}m)\")\n",
    "ax.plot(df[\"date\"], trend_cn, linestyle=STYLE_CN, linewidth=TREND_WIDTH, color=COLOR_CN,\n",
    "        label=f\"China (trend, {WIN}m)\")\n",
    "\n",
    "# Axes labels (no title requested)\n",
    "ax.set_xlabel(\"\", fontsize=LABEL_SIZE)\n",
    "ax.set_ylabel(\"Grievance-oriented framing (0–1)\", fontsize=LABEL_SIZE)\n",
    "\n",
    "# Tick sizes\n",
    "ax.tick_params(axis=\"both\", labelsize=TICK_SIZE)\n",
    "\n",
    "# -----------------------------\n",
    "# X-axis: show every year\n",
    "# -----------------------------\n",
    "years = pd.date_range(df[\"date\"].min(), df[\"date\"].max(), freq=\"YS\")\n",
    "ax.set_xticks(years)\n",
    "ax.set_xticklabels([d.year for d in years], rotation=90)\n",
    "\n",
    "# Grid (subtle)\n",
    "ax.grid(True, linestyle=\"--\", alpha=0.3)\n",
    "\n",
    "# Legend\n",
    "ax.legend(fontsize=LEGEND_SIZE, frameon=True)\n",
    "\n",
    "# Y-limits with headroom for annotations\n",
    "y_min = min(y_nk.min(), y_cn.min())\n",
    "y_max = max(y_nk.max(), y_cn.max())\n",
    "pad = 0.10\n",
    "ax.set_ylim(max(0, y_min - 0.05), min(1, y_max + pad))\n",
    "\n",
    "# -----------------------------\n",
    "# Event annotations (grey text + arrows)\n",
    "# -----------------------------\n",
    "events = [\n",
    "    (\"2006-10-01\", \"NK: Nuclear test\"),\n",
    "    (\"2010-11-01\", \"NK: Yeonpyeong\"),\n",
    "    (\"2013-02-01\", \"NK: Nuclear test\"),\n",
    "    (\"2016-07-01\", \"CN: THAAD retaliation\"),\n",
    "    (\"2017-09-01\", \"NK: Nuclear test\"),\n",
    "    (\"2020-03-01\", \"CN: COVID-19\"),\n",
    "    (\"2022-10-01\", \"CN: Tech controls\"),\n",
    "]\n",
    "\n",
    "top_y = ax.get_ylim()[1]\n",
    "text_y_levels = [top_y - 0.23, top_y - 0.07, top_y - 0.18, top_y - 0.09,\n",
    "                 top_y - 0.37, top_y - 0.06, top_y - 0.09]\n",
    "row = 0\n",
    "\n",
    "for date_str, label in events:\n",
    "    d = pd.to_datetime(date_str)\n",
    "    idx = (df[\"date\"] - d).abs().idxmin()\n",
    "    x_point = df.loc[idx, \"date\"]\n",
    "\n",
    "    y_point = float(y_nk.loc[idx]) if label.startswith(\"NK\") else float(y_cn.loc[idx])\n",
    "    y_text = text_y_levels[row % len(text_y_levels)]\n",
    "    row += 1\n",
    "\n",
    "    ax.annotate(\n",
    "        label,\n",
    "        xy=(x_point, y_point),\n",
    "        xytext=(x_point, y_text),\n",
    "        ha=\"center\",\n",
    "        va=\"bottom\",\n",
    "        fontsize=12,\n",
    "        color=\"gray\",\n",
    "        arrowprops=dict(arrowstyle=\"->\", color=\"gray\", lw=1.2,\n",
    "                        shrinkA=0, shrinkB=4),\n",
    "        clip_on=False\n",
    "    )\n",
    "\n",
    "# Improve layout\n",
    "plt.tight_layout(rect=[0, 0, 1, 0.95])\n",
    "plt.savefig(\"Figure_2.jpg\", dpi=600)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ca597fd-3a28-4073-b213-fb8a6894546a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "919387ad-529f-4a71-aeaf-722dbf2b601b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
